﻿using System;
using System.Web.UI;
using MdbWatch;

public partial class Mdbw_EditDBTable : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
          
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            string tablename = "U_" + StringHelper.ReplaceBadChar(txtTableName.Text);
            string sql = "create table "+tablename +"(";
            if (chklColumn.Items[0].Selected)
            {
                sql += "id AUTOINCREMENT ,PRIMARY KEY (id),";
            }
            if (chklColumn.Items[1].Selected)
            {
                sql += "userid int,";
            }
            if (chklColumn.Items[2].Selected)
            {
                sql += "adminid int,";
            }
            if (chklColumn.Items[3].Selected)
            {
                sql += "addtime datetime default now(),";
            }
            if (chklColumn.Items[4].Selected)
            {
                sql += "uptime datetime default now(),";
            }
            if (sql.EndsWith(","))
            {
                sql = sql.Remove(sql.Length - 1, 1);
            }
            sql += ")";

           DbHelper.ExecuteSql(sql);

            if(!string.IsNullOrEmpty(txtBrief.Text.Trim()))
            {
                //添加说明
                ChangeDesc(tablename, txtBrief.Text.Trim());
            }
           lblResult.Text = "操作成功!";
           ClientScript.RegisterClientScriptBlock(this.GetType(), "", "Finish()", true);
        }
        catch
        {
            lblResult.Text = "创建失败,可能表已经存在!";
        }

    }
    private void ChangeDesc(string tablename,string description)
    {
        DAO.Workspace DAOWorkspace;
        DAO.Database DAODatabase;
        DAO.DBEngine DAODBEngine = new DAO.DBEngine();    
        DAO.TableDef DAOTable;
        // 创建一个工作区 
        DAOWorkspace = DAODBEngine.CreateWorkspace("WorkSpace", "admin", "", 2);
        // 打开数据库 
        string dbpath = Server.MapPath("~/app_data/db.mdb");
        DAODatabase = DAOWorkspace.OpenDatabase(dbpath, false, false, null);

        DAOTable = DAODatabase.TableDefs[tablename];
        try
        {
            DAOTable.Properties["Description"].Value = description;
        }
        catch
        {
            DAO.Property dbProperty = DAOTable.CreateProperty("Description", DAO.DataTypeEnum.dbText, description, false);
            DAOTable.Properties.Append(dbProperty);
            DAOTable.Properties.Refresh();
        }
        finally
        {
            DAODatabase.Close();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(DAOTable);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(DAOWorkspace);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(DAODBEngine);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(DAODatabase);
            DAOTable = null;
            DAOWorkspace = null;
            DAOWorkspace = null;
            DAODatabase = null;
        }
        GC.WaitForPendingFinalizers();
        GC.Collect();
    }

    protected override void OnInit(EventArgs e)
    {
        if (!IsPostBack)
        {
            //添加访问权限的逻辑,session和server.avariable["refer"]以及主机名称.防止跨域提交表单
            if (Request.ServerVariables["HTTP_Referer"] == null || Request.ServerVariables["REMOTE_HOST"] == null || Request.ServerVariables["SERVER_NAME"] != Request.Url.Host)
            {
                Response.Write("非法访问!");
                Response.End();
                return;
            }
        }
    }

}
